package com.wxw.BaseLibrary.tool;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by david on 2017-11-28.
 */
public class L {
    public static String APP = "";
    public static String CurrentPath = SystemTool.getSDCardPath() + "BaseLibrary/Logs";
    public static DocumentBuilder builder = null;

    static {
        if (!"".equals(APP)) {
            CurrentPath = APP + (APP.endsWith("/")? "": "/") + "Logs";
        }
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

    public static synchronized void WriteLog(String msg)
    {
        WriteLog(CurrentPath, msg);
    }

    public static synchronized void WriteLog(String msg, HashMap<String, String> ht)
    {
        WriteLog(CurrentPath, msg, ht, true);
    }

    public static synchronized void WriteLog(String Path, String msg)
    {
        WriteLog(Path, msg, null, true);
    }

    public static synchronized void Only_WriteLog(String msg) {
        WriteLog(CurrentPath, msg, null, false);
    }

    public static synchronized void WriteLog(String Path, String msg, HashMap<String, String> ht, boolean show)
    {
        String file = Path + "/" + ToolsDate.getDate("yyyy-MM-dd") + "_Message.xml";
        try
        {
            File f = new File(Path);
            if (!f.exists()) {
                f.mkdirs();
            }

            f = new File(file);
            if (!f.exists()) {
                String content = "<?xml version='1.0' encoding='utf-8' ?>";
                content += "<Logs/>";
                CreateXmlFile(file, content);
            }
            f = new File(file);
            Document xmlDoc = builder.parse(f);

            Element root = xmlDoc.getDocumentElement();
            Element temp = xmlDoc.createElement("Log");
            if (ht != null && ht.size() > 0)
            {
                Iterator<String> it = ht.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    temp.setAttribute(key, ht.get(key));
                }
            }
            temp.setAttribute("LogDateTime", ToolsDate.getDateLine());
            temp.setTextContent(msg);
            root.appendChild(temp);
            XmlHelper.write2Xml(xmlDoc, file);
        }
        catch (Exception e)
        {
        }
        if (show) {
            System.out.println(ToolsDate.getDateLine() + msg);
        }
    }

    public static synchronized void WriteExceptionLog(String classname, String methodname, String msg)
    {
        HashMap<String, String> ht = CreateHashtable("LogType", "Exception");
        ht.put("ClassName", classname);
        ht.put("MethodName", methodname);
        WriteLog(CurrentPath, msg, ht, true);
    }

    private static synchronized void CreateXmlFile(String fileName, String content)
    {
        try {
            File file = new File(fileName);
            file.createNewFile();
            FileWriter fw = new FileWriter(fileName);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.flush();
            bw.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static synchronized HashMap<String, String> CreateHashtable(String key, String value)
    {
        HashMap<String, String> ht = new HashMap<String, String>();
        ht.put(key, value);
        return ht;
    }

    public static synchronized String readLog() {
        String file = CurrentPath + "/" + ToolsDate.getDate("yyyy-MM-dd") + "_Message.xml";
        return readLog(file);
    }

    public static synchronized String readLog(String Path) {
        String file = Path + "/" + ToolsDate.getDate("yyyy-MM-dd") + "_Message.xml";
        try
        {
            File f = new File(file);
            if (!f.exists()) {
                return "";
            }

            return FileUtil.ReadFile(file);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "";
        }
    }
}
